#ifndef JULIANNA_H
#define JULIANNA_H

#ifdef __cplusplus
extern "C"
{
#endif
#ifdef in_PC
#include "../main.h"
#include "../Lily/Lily_help.h"
#else
#include "LCD/lcd.h"
#include "Lily_help.h"
#define Screen_width 480
#define Screen_height 800
#endif


typedef void *pint;

//#ifdef x64
//	typedef void *pint;
//#else
//typedef int pint;
//#endif // x64

	typedef union
	{
		struct
		{
			int x, y;
		};
		int val[2];
	} V2b_t;
	typedef V2b_t *V2b;

	typedef union
	{
		struct
		{
			int x, y, z;
		};
		int val[3];
	} V3b_t;
	typedef V3b_t *V3b;

	typedef union
	{
		struct
		{
			int x, y, u, v;
		};
		int val[4];
	} V4b_t;
	typedef V4b_t *V4b;

	typedef enum
	{
		None = 0,
		PressedDown = 1,
		Moving = 2,
		PressedUp = 3,
		MoveIn = 4,
		MoveOut = 5,
		Pressed, //长按触发
	} Presse_Statue;

	typedef Presse_Statue TouchEvent;

	typedef enum
	{
		OnCreate = 1, // 视图创建时绘制
		OnTouchEvent, // 响应事件
		ByPass,		  // 内部更新
	} View_Statue;

	typedef View_Statue ViewEvent;

	typedef struct
	{
		int x, y, width, height;
	} Gem_t;
	typedef Gem_t *Gem;

	typedef struct
	{
		union
		{
			Gem_t gem;
			struct
			{
				int x, y;		   //左上角坐标
				int width, height; //尺寸
			};
		};
		pint id;
	} Layout_item_t;
	typedef Layout_item_t *Layout_item;

	// Juli 的交互逻辑
	// 交互事件程序
	typedef struct UIC
	{
		union
		{
			Gem_t gem;
			struct
			{
				int x, y;		   //左上角坐标
				int width, height; //尺寸
			};
		};
		pint id;
		//pint(*pressed_event)(struct UIC* this_, int x, int y, Presse_Statue mode);
		//// OnTouching pressed_event;
		//void (*on_draw)(struct UIC* this_);

		struct UIC *(*onTouching)(struct UIC *this_, int x, int y, TouchEvent events);
		void (*onDrawing)(struct UIC *this_, ViewEvent events);

		Li_List items; //子集目录 List<Layout_item_t>
		// onDraw on_draw;
		char hide;
	} UI_compent_t;
	typedef UI_compent_t *JUI;

	/// <summary>
	/// 处理点击事件
	/// x,y 触发坐标(相对值)
	/// mode：触发模式，PressedDown，Moving，PressedUp，MoveIn...
	/// return pint: 更新视图id
	/// </summary>
	typedef JUI (*OnTouching)(JUI, int, int, TouchEvent);
	/// <summary>
	/// 绘制事件
	/// </summary>
	typedef void (*OnDrawing)(JUI, ViewEvent);

	extern volatile JUI _this;
	extern Li_List Julianna; //控件管理, list of JUI
	extern int _xoff, _yoff; // 坐标偏移

	// GUIs
#define uX(x0) ((x0) + _xoff)
#define uY(x0) ((x0) + _yoff)

#define Juli_draw_circle(x0, y0, r) LCD_Draw_Circle(uX(x0), uY(y0), r)
#define Juli_draw_line(x1, y1, x2, y2) LCD_DrawLine(uX(x1), uY(y1), uX(x2), uY(y2))						//画线
#define Juli_draw_rectangle(x1, y1, w, h) LCD_DrawRectangle(uX(x1), uY(y1), uX(x1) + (w), uY(y1) + (h)) //画矩形
#define Juli_fill(sx, sy, width, height, color) LCD_fill_with(uX(sx), uY(sy), width, height, color)
// LCD_Fill(uX(sx), uY(sy), uX(ex), uY(ey), color)
#define Juli_draw_point(x, y) LCD_DrawPoint(uX(x), uY(y))				   //画点
#define Juli_draw_point_color(x, y, c) LCD_Fast_DrawPoint(uX(x), uY(y), c) //画点

#define Juli_read_point(x, y) LCD_ReadPoint(uX(x), uY(y))
#define Juli_put_text(x, y, text) LCD_printAt(uX(x), uY(y), text)
#define Juli_put_char(x, y, c) LCD_putcAt(uX(x), uY(y), c)

#define Juli_fill_circle(x, y, r, c) \
	ug_FillCircle(uX(x), uY(y), r, c)
#define Juli_draw_arc(x, y, r, s, c) \
	ug_DrawArc(uX(x), uY(y), r, s, c)
	//#define Juli_fill_round_rect(x, y, w, h, r, c) \
//	ug_FillRoundFrame(uX(x), uY(y), uX(x) + w/* - r*/, uY(y) + h, r, c)
	#define Juli_fill_circle(x, y, r, c) \
	ug_FillCircle(uX(x), uY(y), r, c)

	
	void Juli_fill_round_rect(int x, int y, int w, int h, int r, int c);
	void Juli_draw_round_rect(int x, int y, int w, int h, int r);

	//#define Juli_draw_round_rect(x, y, w, h, r) \
//	ug_DrawRoundFrame(uX(x), uY(y), uX(x) + (w) , uY(y) + (h), r)

	void init_julianna(void);
	void juli_show_up(void);

	//pint get_activity_id();
	JUI public_a_ui_compent_id(int x, int y, int width, int height, pint id, OnTouching hook, OnDrawing draw);
	int juli_boardcast_event(int x, int y, Presse_Statue event);
	//pint public_a_ui_compent(int x, int y, int width, int height, OnTouching hook, OnDrawing draw);
	void juli_show_compent(pint id, ViewEvent e);
	//void let_juli_call(int id);
	void juli_show_coment_index(int i, ViewEvent e);
	void juli_show_ui(JUI ui, ViewEvent events);

	/// <summary>
	/// creare new UI, and add to Juli
	/// </summary>
	/// <param name="gem">gemmetry location</param>
	/// <param name="id"></param>
	/// <param name="onTouch"></param>
	/// <param name="onDraw"></param>
	/// <returns>ui handle, NULL if error</returns>
	JUI new_ui(Gem gem, pint id, OnTouching onTouch, OnDrawing onDraw);
	/// <summary>
	/// create new sub_items to a layout
	/// </summary>
	/// <param name="layout"></param>
	/// <param name="gem"></param>
	/// <param name="id"></param>
	/// <returns>ui handle, NULL if error occurs</returns>
	Layout_item new_item_to(JUI layout, Gem gem, pint id);

	int remove_ui(JUI ui);

	int cmd_juli(int n, char *arg[]);

	//----------------- About Cursor ------------
	typedef struct
	{
		void *arg;
		TasksArg_def shineTask;
		void (*loseFocusCallBack)();
	} CursorItem_t;
	typedef CursorItem_t *CursorItem;
	extern CursorItem cursor_current_item;
	// to just change the cursor with yours
	// apart from that, nothing else will be done in this function
	// so focus your ui, change the pipes
	void cursor_push_in(void *arg, TasksArg_def task, void (*loseFocusCallBack)(void *));
	void delete_cursorItem(void);
	// when a ui is destoried, and it used a cursor,
	// the ui is supposed to remove its cursor
	int delete_if_its_your_cursor(void *arg, TasksArg_def task);
	int window_split(Gem window, char *split, Gem out_gem, int margain);
	void window_expand_y(JUI window_ui, int m);
	void window_expand_x(JUI window_ui,int m);
	void window_expand(JUI window_ui,int my, int mx);

#ifdef __cplusplus
}
#endif
#endif
